import 'dart:convert';

import 'package:app_review/app_review.dart';
import 'package:awesome_dialog/awesome_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_reader/dao/login_data_manager.dart';
import 'package:flutter_reader/dao/pay_data_manager.dart';
import 'package:flutter_reader/model/pay/pay_config_model.dart';
import 'package:flutter_reader/pages/Login/login_nav_page.dart';
import 'package:flutter_reader/pages/home/recharge/Radio_Widget.dart';
import 'package:flutter_reader/pages/home/recharge/protocol_dialog.dart';
import 'package:flutter_reader/pages/home/recharge/radio_model.dart';
import 'package:flutter_reader/pages/read_book/book_introduction.dart';
import 'package:flutter_reader/tools/color_loader.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import 'package:fluwx/fluwx.dart' as fluwx;
import 'package:overlay_support/overlay_support.dart';
import 'package:flutter_reader/tools/globals.dart' as globals;
import 'package:shared_preferences/shared_preferences.dart';
import 'package:tobias/tobias.dart' as tobias;

import 'Radio_payment.dart';

class RechargePage extends StatefulWidget {
  RechargePage({Key key}) : super(key: key);

  @override
  _RechargePageState createState() {
    return _RechargePageState();
  }
}

class _RechargePageState extends State<RechargePage> {
  bool _haveReadProtocol;
  bool _checkboxListChecked;

  String _selectedPayment;

  bool _selectWechat = true;
  bool _selectAlipay = false;
  int _selectPay = 1;

  bool _isLoadingConfig = true;
  int coin = 0;
  List<PayConfigItem> _itemList = [];

  List<String> _buttonLables = [];
  List<String> _buttonSubLables = [];
  List<String> _buttonRemarksLables = [];
  List<String> _buttonLogo = [];
  List<String> _buttonCode = [];


  @override
  void initState() {
    loadConfig();
    _haveReadProtocol = true;
    _checkboxListChecked = true;
    getAppID();
    // initPlatformStateUmeng();
    super.initState();
    // LcfarmFlutterUmeng.onResume();
    // LcfarmFlutterUmeng.beginLogPageView('recharge');
  }

  @override
  void dispose() {
    super.dispose();
    // LcfarmFlutterUmeng.onPause();
    // LcfarmFlutterUmeng.endLogPageView('recharge');
  }

  ///集成极光推送
  String debugLable = 'Unknown';
  // final JPush jpush = new JPush();

  // Future<void> initPlatformStateUmeng() async {
  //   String platformVersion;
  //   try {
  //     jpush.addEventHandler(
  //       onOpenNotification: (Map<String, dynamic> message) async {
  //         print('通知栏打开');
  //         print("flutter onOpenNotification: $message");
  //         print("${message["extras"]}");
  //         print("${message["extras"]["cn.jpush.android.EXTRA"]}");
  //         Map aa = json.decode(message["extras"]["cn.jpush.android.EXTRA"]);
  //         print("${aa["bookID"]}");
  //         Navigator.push(context, MaterialPageRoute(
  //             builder: (context) => BookInfoPage(bookId: aa["bookID"],channel: "1",)
  //         ));
  //       },
  //     );
  //   } on PlatformException {
  //     platformVersion = 'Failed to get platform version.';
  //   }
  // }

  ///动态获取AppId
  getAppID(){
    print('error:${globals.dateError}');
    LoginDao.fetchAppID(globals.dateError).then((value){
      print(value['data']['appid']);
      _initFluwx();
    });
  }

  ///fluwx初始化
  _initFluwx() async{
    await fluwx.registerWxApi(appId: 'wx6ee2ab986309f9b4',doOnAndroid: true,doOnIOS: true,universalLink: 'https://www.kxyd.com.cn');
    var result = await fluwx.isWeChatInstalled();
    print("is installed $result");
  }

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {}


  loadConfig(){
    _itemList.clear();
    _buttonLables.clear();
    _buttonSubLables.clear();
    _buttonRemarksLables.clear();
    _buttonLogo.clear();
    _buttonCode.clear();
    PayDao.fetchConfig().then((value){
      print('value-----${value}');

      setState(() {
      coin = value.data.coin;
      for(int i = 0;i < value.data.item.length; i++){
          _itemList.add(value.data.item[i]);
      }
      print(_itemList.length);
      for(int i = 0;i < _itemList.length; i++){
        _buttonLables.add(_itemList[i].price);
        _buttonSubLables.add(_itemList[i].name);
        _buttonRemarksLables.add(_itemList[i].desc);
        _buttonCode.add(_itemList[i].code);
        if(_itemList[i].logo == 'hot'){
          _buttonLogo.add('hot');
        }
        else{
          _buttonLogo.add('cold');
        }
      }
      print(_buttonLogo);
      print(_buttonLables);
      print(_buttonCode);
      _isLoadingConfig = false;
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      backgroundColor: Colors.white,
      body: _isLoadingConfig ?
      Container(
        width: ScreenUtil.screenWidth,
        height: ScreenUtil.screenHeight,
        child: Center(
          child: Container(
            padding: EdgeInsets.only(top: ScreenUtil().setHeight(1000)),
            child: Column(
              children: <Widget>[
                ColorLoader(
                  color1: Colors.redAccent,
                  color2: Colors.green,
                  color3: Colors.amber,
                ),
                Container(
                  margin: EdgeInsets.only(top: ScreenUtil().setHeight(60)),
                  child: Text(
                      '正在为您生成签到页...'
                  ),
                )
              ],
            ),
          ),
        ),
      ) : Container(
        child:
        Column(
          children: <Widget>[
            Container(
              height: ScreenUtil().setHeight(2200),
              child: MediaQuery.removePadding(
                  context: context,
                  removeTop: true,
                  child: ListView(
                      children: <Widget>[
                        _coinWidget(),
                        _chooseWidget(),
                        _showProtocolButton(),
                        _payment(),
                      ]
                  )),
            ),
            _confirmButton(),
          ],
        ),
      ),
    );
  }

  _coinWidget() {
    return Container(
      decoration: BoxDecoration(
        color: Color(0xffe53935),
      ),
      margin: EdgeInsets.fromLTRB(0, 0, 0, ScreenUtil().setHeight(40)),
      height: ScreenUtil().setHeight(500),
      child: Column(
        children: <Widget>[
          Container(
            margin: EdgeInsets.only(top: ScreenUtil().setHeight(130)),
            child: Stack(
              children: <Widget>[
                IconButton(
                  icon: Icon(
                    Icons.arrow_back_ios,
                    color: Colors.white,
                  ),
                  onPressed: _backToHome,
                ),
                Container(
                  padding: EdgeInsets.only(top: ScreenUtil().setHeight(30)),
                  child: Center(
                    child: Text(
                      '充值中心',
                      style: TextStyle(
                          color: Colors.white,
                          fontSize: ScreenUtil().setSp(65),
                          fontWeight: FontWeight.w600),
                    ),
                  ),
                )
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(top: ScreenUtil().setHeight(40)),
            child: Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(60)),
                  child: Image(
                    image: AssetImage('images/fl_coin.png'),
                    width: ScreenUtil().setWidth(100),
                    height: ScreenUtil().setHeight(100),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(40)),
                  child: Text(
                    '当前余额：   ',
                    style: TextStyle(
                        color: Colors.white, fontSize: ScreenUtil().setSp(60)),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(5)),
                  child: Text(
                    '${coin}      书币',
                    style: TextStyle(
                        color: Colors.white, fontSize: ScreenUtil().setSp(60)
                    ),
                  ),
                )
              ],
            ),
          )
        ],
      ),
    );
  }




  _chooseWidget() {
    return Container(
      decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.all(Radius.circular(5))),
      margin: EdgeInsets.only(left:ScreenUtil().setWidth(40),
          right:ScreenUtil().setWidth(40)),
      height: ScreenUtil().setHeight(300) * (_buttonLables.length == 7 ? 5 : 4),
      width: ScreenUtil().setWidth(1100),
      child: Container(
        child: CustomRadioButton(
          hight: ScreenUtil().setHeight(260),
          width: ScreenUtil().setWidth(450),
          buttonColor: Colors.white,
          buttonLables: _buttonLables,
          buttonSubLables: _buttonSubLables,
          buttonLogo: _buttonLogo,
          buttonValues: _buttonLables.length == 6 ? ['1', '2', '3', '4', '5', '6'] : ['1', '2', '3', '4', '5', '6','7'],
          buttonRemarksLables: _buttonRemarksLables,
          radioButtonValue: (value){
            print(value);
            _selectPay = int.parse(value) - 1;
          },
          selectedColor: Color(0xffe53935),
        ),
      ),
    );
  }

  _showProtocolButton() {
    return Center(
      child: Container(
        height: ScreenUtil().setHeight(150),
        child: MaterialButton(
          child: Container(
              child: Container(
            margin: EdgeInsets.only(left: ScreenUtil().setWidth(40)),
            child: CheckboxListTile(
              onChanged: (isCheck) {
                isCheck == true ? _showProtocol() : null;
                setState(() {
                  _checkboxListChecked = isCheck;
                });
              },
              selected: false,
              value: _checkboxListChecked,
              title: Container(
                child: Row(
                  children: <Widget>[
                    Text(
                      '我已阅读并同意 ',
                      style: TextStyle(fontSize: ScreenUtil().setSp(40)),
                    ),
                    Text.rich(TextSpan(
                      text: ' 充值条款声明',
                      style: TextStyle(
                          fontSize: ScreenUtil().setSp(40),
                          color: Colors.brown,
                          decoration: TextDecoration.underline),
                    ))
                  ],
                ),
              ),
              controlAffinity: ListTileControlAffinity.leading,
            ),
          )),
        ),
      ),
    );
  }


  _payment() {
    return Container(
      margin: EdgeInsets.only(top: ScreenUtil().setHeight(20)),
      child: Column(
        children: <Widget>[
          Container(
            decoration: BoxDecoration(
              border: Border(bottom: BorderSide(
                width: 1,
                color: Colors.black12
              ))
            ),
            child: Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(40)),
                  child: Image(
                    width: ScreenUtil().setWidth(100),
                    image: AssetImage('images/fl_WeChat.png'),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(50)),
                  child: Text('微信支付'),
                ),
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(600)),
//                  child: CircularCheckBox(
//                      value: _selectWechat,
//                      materialTapTargetSize: MaterialTapTargetSize.padded,
//                      onChanged: (bool x) {
//                        setState(() {
//                          _selectAlipay = !_selectAlipay;
//                          _selectWechat = !_selectWechat;
//                        });
//                      }),
                )
              ],
            ),
          ),
          Container(
            decoration: BoxDecoration(
                border: Border(bottom: BorderSide(
                    width: 1,
                    color: Colors.black12
                ))
            ),
            child: Row(
              children: <Widget>[
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(40)),
                  child: Image(
                    width: ScreenUtil().setWidth(100),
                    image: AssetImage('images/fl_AliPay.png'),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(50)),
                  child: Text('支付宝支付'),
                ),
                Container(
                  margin: EdgeInsets.only(left: ScreenUtil().setWidth(560)),
//                  child: CircularCheckBox(
//                      value: _selectAlipay,
//                      materialTapTargetSize: MaterialTapTargetSize.padded,
//                      onChanged: (bool x) {
//                        setState(() {
//                          _selectAlipay = !_selectAlipay;
//                          _selectWechat = !_selectWechat;
//                        });
//                      }),
                )
              ],
            ),
          )
        ],
      ),
    );
  }

  pay(String data) async{
    var result = await await tobias.aliPay(data);
    print('result:${result}');
    loadConfig();
  }

  _confirmButton() {
    return Expanded(
      child: InkWell(
          onTap: () async{
            SharedPreferences sharedPreferences = await SharedPreferences.getInstance();
            if(sharedPreferences.getString("login")=="true") {
              print('确认支付');
              print(_selectAlipay);
              print(_buttonCode[_selectPay]);
              print(globals.fullSessionID);
              if (_selectAlipay) {
                PayDao.confirmAliPay(
                    globals.fullSessionID, _buttonCode[_selectPay]).then((
                    value) {
                  print(value['data']);
                  pay(value['data']);
                });
              }
              else {
                PayDao.confirmWechatPay(
                    globals.fullSessionID, _buttonCode[_selectPay],
                    globals.dateError).then((value) async {
                  print(value);
                  var isinstall = await fluwx.isWeChatInstalled();
                  print('is installed = $isinstall');
                  fluwx.payWithWeChat(
                      appId: value['data']['appid'].toString(),
                      partnerId: value['data']['partnerid'].toString(),
                      prepayId: value['data']['prepayid'].toString(),
                      packageValue: value['data']['package'].toString(),
                      nonceStr: value['data']['noncestr'].toString(),
                      timeStamp: int.parse(value['data']['timestamp']),
                      sign: value['data']['sign'].toString()
                  ).then((data) {
                    print('data:$data');
                  });
                  print('支付结束');
                  fluwx.responseFromPayment.listen((response) {
                    print('errorCode:${response.errCode}');
                    print('${response.errStr}');
                  });
                });
              }
            }
            else {
              print("先登录");
              Navigator.push(context, MaterialPageRoute(
                  builder: (context) => LoginNavPage()
              )).then((value){
                loadConfig();
              });
            }
          },
          child: Container(
            width: ScreenUtil().setWidth(1125),
            color: Color(0xffe53935),
            child: Center(
              child: Text(
                '确认支付',
                style: TextStyle(
                    color: Colors.white,
                    fontWeight: FontWeight.w500,
                    fontSize: ScreenUtil().setSp(50)),
              ),
            ),
          )
      ),
    );
  }

  _backToHome() {
    Navigator.pop(context);
  }

  void _showProtocol() {
    showDialog(
        context: context,
        barrierDismissible: true,
        builder: (BuildContext context) {
          return ProtocolDialog(
            title: '充值条款说明',
            dialogText:
                '1.充值兑换比例：1元=100书币\n2.书券可用于兑换VIP，进入“个人中心”点击“书券”即可进入兑换页面！\n3.充得多，送得多（PS：购买包年会员更划算哦！）\n4.充值包年、包月VIP成为VIP会员，可免费查看所有书籍\n5.充值后书币到账有延迟，30分钟内未到账 请联系客服 QQ:kanxiaoshuo101\n6.由于小说书豆为特殊虚拟商品，一旦成功购买即开始享受部分及全部服务，因此不可退换，请购买时注意',
          );
        });
  }

}
